//
//  WWCartoonCollectionCell.m
//  YiQiTingShu
//
//  Created by WeiWei on 15/11/11.
//  Copyright © 2015年 直到世界的尽头. All rights reserved.
//

#import "WWCartoonCollectionCell.h"
#import "PublicHeader.h"
#import "WWCartoonTableCell.h"
#import "WWCartoonModel.h"
#import <MBProgressHUD.h>

@interface WWCartoonCollectionCell ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) MBProgressHUD *progressView;

@end

@implementation WWCartoonCollectionCell

- (void)getData {
    [AFTools getDataByURLString:@"http://acg.18touch.com/api/special?ak=&channel=AppStore&dk=9948b92dafe446718b4842c71e9b374825d15d41&lastId=4082&os=iOS&v=1.6.1" WithDataBlock:^(id data) {
        NSArray *tempArray = [NSArray arrayWithArray:[data objectForKey:@"data"]];
        for (NSDictionary *dic in tempArray) {
            WWCartoonModel *model = [WWCartoonModel wwModelWithDictionary:dic];
            [self.dataArray addObject:model];
        }
        [self.tableView reloadData];
        [_progressView removeFromSuperview];
    }];
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
        self.progressView = [[MBProgressHUD alloc]initWithView:self.tableView];
        _progressView.labelText = @"努力加载中,请稍等";
        [_progressView setMode:MBProgressHUDModeIndeterminate];
        [_progressView show:YES];
        _progressView.backgroundColor = [UIColor colorWithRed:0.58 green:0.6 blue:0.61 alpha:0.3];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[WWCartoonTableCell class] forCellReuseIdentifier:@"cell"];
        self.dataArray = [NSMutableArray array];
        [self getData];
        _tableView.rowHeight = FitConstant_YorHeight(220);
        [self.contentView addSubview:_tableView];
        [self.tableView NightWithType:UIViewColorTypeNormal];
        [self.contentView addSubview:_progressView];
    }
    return self;
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WWCartoonTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (self.dataArray.count) {
        WWCartoonModel *model = self.dataArray[indexPath.row];
        cell.model = model;
    }
    return cell;
}

@end
